home *** CD-ROM | disk | FTP | other *** search
/ Linux Cubed Series 2: Applications / Linux Cubed Series 2 - Applications.iso / editors / emacs / xemacs / xemacs-1.006 / xemacs-1 / lib / xemacs-19.13 / lisp / prim / paragraphs.el < prev    next >
Encoding:
Text File  |  1995-05-12  |  9.3 KB  |  251 lines

  1. ;;; paragraphs.el --- paragraph and sentence parsing.
  2.  
  3. ;; Copyright (C) 1985, 1986, 1987, 1991, 1993, 1994
  4. ;; Free Software Foundation, Inc.
  5.  
  6. ;; Maintainer: FSF
  7. ;; Keywords: wp
  8.  
  9. ;; This file is part of XEmacs.
  10.  
  11. ;; XEmacs is free software; you can redistribute it and/or modify it
  12. ;; under the terms of the GNU General Public License as published by
  13. ;; the Free Software Foundation; either version 2, or (at your option)
  14. ;; any later version.
  15.  
  16. ;; XEmacs is distributed in the hope that it will be useful, but
  17. ;; WITHOUT ANY WARRANTY; without even the implied warranty of
  18. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  19. ;; General Public License for more details.
  20.  
  21. ;; You should have received a copy of the GNU General Public License
  22. ;; along with XEmacs; see the file COPYING.  If not, write to the Free
  23. ;; Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  24.  
  25. ;;; Synched up with: FSF 19.28.
  26.  
  27. ;;; Commentary:
  28.  
  29. ;; This package provides the paragraph-oriented commands documented in the
  30. ;; XEmacs Reference Manual.
  31.  
  32. ;;; Code:
  33.  
  34. (defconst paragraph-start (purecopy "^[ \t\n\f]") "\
  35. *Regexp for beginning of a line that starts OR separates paragraphs.
  36. This regexp should match lines that separate paragraphs
  37. and should also match lines that start a paragraph
  38. \(and are part of that paragraph).
  39. The variable `paragraph-separate' specifies how to distinguish
  40. lines that start paragraphs from lines that separate them.")
  41.  
  42. (defconst paragraph-separate "^[ \t\f]*$" "\
  43. *Regexp for beginning of a line that separates paragraphs.
  44. If you change this, you may have to change paragraph-start also.")
  45.  
  46. (defconst sentence-end (purecopy "[.?!][]\"')}]*\\($\\| $\\|\t\\|  \\)[ \t\n]*") "\
  47. *Regexp describing the end of a sentence.
  48. All paragraph boundaries also end sentences, regardless.
  49.  
  50. In order to be recognized as the end of a sentence, the ending period,
  51. question mark, or exclamation point must be followed by two spaces,
  52. unless it's inside some sort of quotes or parenthesis.")
  53.  
  54. (defconst page-delimiter (purecopy "^\014") "\
  55. *Regexp describing line-beginnings that separate pages.")
  56.  
  57. (defvar paragraph-ignore-fill-prefix nil "\
  58. Non-nil means the paragraph commands are not affected by `fill-prefix'.
  59. This is desirable in modes where blank lines are the paragraph delimiters.")
  60.  
  61. (defun forward-paragraph (&optional arg)
  62.   "Move forward to end of paragraph.
  63. With arg N, do it N times; negative arg -N means move backward N paragraphs.
  64.  
  65. A line which `paragraph-start' matches either separates paragraphs
  66. \(if `paragraph-separate' matches it also) or is the first line of a paragraph.
  67. A paragraph end is the beginning of a line which is not part of the paragraph
  68. to which the end of the previous line belongs, or the end of the buffer."
  69.   (interactive "_p")
  70.   (or arg (setq arg 1))
  71.   (let* ((fill-prefix-regexp
  72.       (and fill-prefix (not (equal fill-prefix ""))
  73.            (not paragraph-ignore-fill-prefix)
  74.            (regexp-quote fill-prefix)))
  75.      (paragraph-separate
  76.       (if fill-prefix-regexp
  77.           (concat paragraph-separate "\\|^"
  78.               fill-prefix-regexp "[ \t]*$")
  79.         paragraph-separate)))
  80.     (while (and (< arg 0) (not (bobp)))
  81.       (if (and (not (looking-at paragraph-separate))
  82.            (re-search-backward "^\n" (max (1- (point)) (point-min)) t))
  83.       nil
  84.     ;; Move back over paragraph-separating lines.
  85.     (forward-char -1) (beginning-of-line)
  86.     (while (and (not (bobp)) (looking-at paragraph-separate))
  87.       (forward-line -1))
  88.     (if (bobp)
  89.         nil
  90.           (progn
  91.             ;; Go to end of the previous (non-separating) line.
  92.             (end-of-line)
  93.             ;; Search back for line that starts or separates paragraphs.
  94.             (if (if fill-prefix-regexp
  95.                     ;; There is a fill prefix; it overrides paragraph-start.
  96.                     (progn
  97.                       (while (progn (beginning-of-line)
  98.                                     (and (not (bobp))
  99.                                          (not (looking-at paragraph-separate))
  100.                                          (looking-at fill-prefix-regexp)))
  101.                         (forward-line -1))
  102.                       (not (bobp)))
  103.                     (re-search-backward paragraph-start nil t))
  104.                 ;; Found one.
  105.                 (progn
  106.                   ;; Move forward over paragraph separators.
  107.                   ;; We know this cannot reach the place we started
  108.                   ;; because we know we moved back over a non-separator.
  109.                   (while (and (not (eobp)) (looking-at paragraph-separate))
  110.                     (forward-line 1))
  111.                   (if (eq (char-after (- (point) 2)) ?\n)
  112.                       (forward-line -1)))
  113.                 ;; No starter or separator line => use buffer beg.
  114.                 (goto-char (point-min))))))
  115.       (setq arg (1+ arg)))
  116.     (while (and (> arg 0) (not (eobp)))
  117.       (beginning-of-line)
  118.       (while (prog1 (and (not (eobp))
  119.              (looking-at paragraph-separate))
  120.             (forward-line 1)))
  121.       (if fill-prefix-regexp
  122.       ;; There is a fill prefix; it overrides paragraph-start.
  123.       (while (and (not (eobp))
  124.               (not (looking-at paragraph-separate))
  125.               (looking-at fill-prefix-regexp))
  126.         (forward-line 1))
  127.     (if (re-search-forward paragraph-start nil t)
  128.         (goto-char (match-beginning 0))
  129.       (goto-char (point-max))))
  130.       (setq arg (1- arg)))))
  131.  
  132. (defun backward-paragraph (&optional arg)
  133.   "Move backward to start of paragraph.
  134. With arg N, do it N times; negative arg -N means move forward N paragraphs.
  135.  
  136. A paragraph start is the beginning of a line which is a
  137. `first-line-of-paragraph' or which is ordinary text and follows a
  138. paragraph-separating line; except: if the first real line of a
  139. paragraph is preceded by a blank line, the paragraph starts at that
  140. blank line.
  141.  
  142. See `forward-paragraph' for more information."
  143.   (interactive "_p")
  144.   (or arg (setq arg 1))
  145.   (forward-paragraph (- arg)))
  146.  
  147. (defun mark-paragraph ()
  148.   "Put point at beginning of this paragraph, mark at end.
  149. The paragraph marked is the one that contains point or follows point."
  150.   (interactive)
  151.   (forward-paragraph 1)
  152.   (push-mark nil t t)
  153.   (backward-paragraph 1))
  154.  
  155. (defun kill-paragraph (arg)
  156.   "Kill forward to end of paragraph.
  157. With arg N, kill forward to Nth end of paragraph;
  158. negative arg -N means kill backward to Nth start of paragraph."
  159.   (interactive "*p")
  160.   (kill-region (point) (progn (forward-paragraph arg) (point))))
  161.  
  162. (defun backward-kill-paragraph (arg)
  163.   "Kill back to start of paragraph.
  164. With arg N, kill back to Nth start of paragraph;
  165. negative arg -N means kill forward to Nth end of paragraph."
  166.   (interactive "*p")
  167.   (kill-region (point) (progn (backward-paragraph arg) (point))))
  168.  
  169. (defun transpose-paragraphs (arg)
  170.   "Interchange this (or next) paragraph with previous one."
  171.   (interactive "*p")
  172.   (transpose-subr 'forward-paragraph arg))
  173.  
  174. (defun start-of-paragraph-text ()
  175.   (let ((opoint (point)) npoint)
  176.     (forward-paragraph -1)
  177.     (setq npoint (point))
  178.     (skip-chars-forward " \t\n")
  179.     ;; If the range of blank lines found spans the original start point,
  180.     ;; try again from the beginning of it.
  181.     ;; Must be careful to avoid infinite loop
  182.     ;; when following a single return at start of buffer.
  183.     (if (and (>= (point) opoint) (< npoint opoint))
  184.     (progn
  185.       (goto-char npoint)
  186.       (if (> npoint (point-min))
  187.           (start-of-paragraph-text))))))
  188.  
  189. (defun end-of-paragraph-text ()
  190.   (let ((opoint (point)))
  191.     (forward-paragraph 1)
  192.     (if (eq (preceding-char) ?\n) (forward-char -1))
  193.     (if (<= (point) opoint)
  194.     (progn
  195.       (forward-char 1)
  196.       (if (< (point) (point-max))
  197.           (end-of-paragraph-text))))))
  198.  
  199. (defun forward-sentence (&optional arg)
  200.   "Move forward to next `sentence-end'.  With argument, repeat.
  201. With negative argument, move backward repeatedly to `sentence-beginning'.
  202.  
  203. The variable `sentence-end' is a regular expression that matches ends
  204. of sentences.  Also, every paragraph boundary terminates sentences as
  205. well."
  206.   (interactive "_p")
  207.   (or arg (setq arg 1))
  208.   (while (< arg 0)
  209.     (let ((par-beg (save-excursion (start-of-paragraph-text) (point))))
  210.       (if (re-search-backward (concat sentence-end "[^ \t\n]") par-beg t)
  211.       (goto-char (1- (match-end 0)))
  212.     (goto-char par-beg)))
  213.     (setq arg (1+ arg)))
  214.   (while (> arg 0)
  215.     (let ((par-end (save-excursion (end-of-paragraph-text) (point))))
  216.       (if (re-search-forward sentence-end par-end t)
  217.       (skip-chars-backward " \t\n")
  218.     (goto-char par-end)))
  219.     (setq arg (1- arg))))
  220.  
  221. (defun backward-sentence (&optional arg)
  222.   "Move backward to start of sentence.  With arg, do it arg times.
  223. See `forward-sentence' for more information."
  224.   (interactive "_p")
  225.   (or arg (setq arg 1))
  226.   (forward-sentence (- arg)))
  227.  
  228. (defun kill-sentence (&optional arg)
  229.   "Kill from point to end of sentence.
  230. With arg, repeat; negative arg -N means kill back to Nth start of sentence."
  231.   (interactive "*p")
  232.   (kill-region (point) (progn (forward-sentence arg) (point))))
  233.  
  234. (defun backward-kill-sentence (&optional arg)
  235.   "Kill back from point to start of sentence.
  236. With arg, repeat, or kill forward to Nth end of sentence if negative arg -N."
  237.   (interactive "*p")
  238.   (kill-region (point) (progn (backward-sentence arg) (point))))
  239.  
  240. (defun mark-end-of-sentence (arg)
  241.   "Put mark at end of sentence.  Arg works as in `forward-sentence'."
  242.   (interactive "p")
  243.   (mark-something 'mark-end-of-sentence 'forward-sentence arg))
  244.  
  245. (defun transpose-sentences (arg)
  246.   "Interchange this (next) and previous sentence."
  247.   (interactive "*p")
  248.   (transpose-subr 'forward-sentence arg))
  249.  
  250. ;;; paragraphs.el ends here
  251.